/*
 * All right reserved@Raygo 2022~2030
 */

package com.raygo.runner.core.entity;

import java.util.HashSet;
import java.util.Set;

import com.raygo.runner.core.utils.DateTimeUtil;
import com.raygo.runner.core.utils.StringUtil;

/**
 * 全流程结果构造器
 *
 * @author Raygo
 * @since 2022年5月26日
 */
public class ResultBuilder {
    /**
     * 结果
     */
    private FlowResult flowResult;

    /**
     * 名称序列
     */
    private Set<String> groupNameSet = new HashSet<>();

    /**
     * 当前组名称
     */
    private String currentGroup;

    /**
     * 构造器
     *
     * @param result
     */
    public ResultBuilder(FlowResult result) {
        this.flowResult = result;
    }

    /**
     * 新增组结果
     *
     * @param groupResult 组结果
     */
    public void addGroupResult(GroupResult groupResult) {
        if (groupResult == null) {
            return;
        }

        String groupName = groupResult.getName();
        if (groupNameSet.contains(groupName)) {
            flowResult.getGroupResult(groupName).setStepResults(groupResult.getStepResults());
        } else {
            flowResult.getGroupResults().add(groupResult);
        }
        currentGroup = groupName;
    }

    /**
     * 新增组结果
     *
     * @param groupResult 组结果
     */
    public void addGroupResult(String groupName) {
        if (StringUtil.isEmpty(groupName)) {
            return;
        }
        if (groupNameSet.contains(groupName)) {
            return;
        }

        // 刷新上一项的时间
        if (StringUtil.isNotEmpty(currentGroup) && !groupName.equalsIgnoreCase(currentGroup)) {
            flowResult.getGroupResult(groupName).setEndTime(DateTimeUtil.getSecond());
            currentGroup = groupName;
        }

        GroupResult groupResult = new GroupResult(groupName);
        groupResult.setBeginTime(DateTimeUtil.getSecond());
        flowResult.getGroupResults().add(groupResult);
        groupNameSet.add(groupName);
    }

    /**
     * 添加步骤结果
     *
     * @param groupName 组名
     * @param stepName  步骤名
     * @param value     值
     * @param result    结果
     * @oaram condition 判断条件
     */
    public void addStepResult(String groupName, String stepName, int result, String value, String condition) {
        if (StringUtil.isEmpty(groupName) || StringUtil.isEmpty(stepName)) {
            return;
        }

        // 刷新上一组的时间
        if (StringUtil.isNotEmpty(currentGroup) && !groupName.equalsIgnoreCase(currentGroup)) {
            flowResult.getGroupResult(groupName).setEndTime(DateTimeUtil.getSecond());
            currentGroup = groupName;
        }

        GroupResult groupResult;
        if (groupNameSet.contains(groupName)) {
            groupResult = flowResult.getGroupResult(groupName);
        } else {
            groupResult = new GroupResult(groupName);
            groupResult.setBeginTime(DateTimeUtil.getSecond());
            flowResult.getGroupResults().add(groupResult);
            groupNameSet.add(groupName);
        }

        StepResult stepResult = new StepResult(stepName, result, value);
        stepResult.setCondition(condition);
        groupResult.getStepResults().add(stepResult);
    }

    /**
     * 完成构建
     *
     * @return the result
     */
    public FlowResult build() {
        return flowResult;
    }
}
